package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.gold.GoldDto;
import com.java110.dto.gold.GoldWithdrawalDto;
import com.java110.intf.acct.IGoldV1InnerServiceSMO;
import com.java110.intf.acct.IGoldWithdrawalV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.po.gold.GoldWithdrawalPo;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("applyGoldWithHoldBmoImpl")
public class ApplyGoldWithHoldBmoImpl implements IMallCommonApiBmo {


    public static final String CODE_PREFIX_ID = "10";

    @Autowired
    private IGoldWithdrawalV1InnerServiceSMO goldWithdrawalV1InnerServiceSMOImpl;

    @Autowired
    private IGoldV1InnerServiceSMO goldV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "goldId", "请求报文中未包含金币ID");
        Assert.hasKeyAndValue(reqJson, "communityId", "请求报文中未包含小区");
        Assert.hasKeyAndValue(reqJson, "applyAmount", "请求报文中未包含申请金额");
        Assert.hasKeyAndValue(reqJson, "applyUserName", "请求报文中未包含未包含申请人");
        Assert.hasKeyAndValue(reqJson, "applyUserTel", "请求报文中未包含申请人电话");
        Assert.hasKeyAndValue(reqJson, "remark", "请求报文中未包含备注信息");
        Assert.hasKeyAndValue(reqJson, "bankNum", "请求报文中未包含银行账号");
        Assert.hasKeyAndValue(reqJson, "bankNumName", "请求报文中未包含银行账号名称");
        Assert.hasKeyAndValue(reqJson, "bankName", "请求报文中未包含开户行");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        String userId = reqJson.getString("propertyUserId");

        GoldDto goldDto = new GoldDto();
        goldDto.setGoldId(reqJson.getString("goldId"));
        goldDto.setCommunityId(reqJson.getString("communityId"));
        List<GoldDto> goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        Assert.listOnlyOne(goldDtos, "金币账户不存在");


        if (Double.parseDouble(goldDtos.get(0).getAmount()) < reqJson.getDouble("applyAmount")) {
            throw new CmdException("金额不足");
        }


        GoldWithdrawalPo goldWithdrawalPo = BeanConvertUtil.covertBean(reqJson, GoldWithdrawalPo.class);
        goldWithdrawalPo.setGwId(GenerateCodeFactory.getGeneratorId(CODE_PREFIX_ID));
        goldWithdrawalPo.setState(GoldWithdrawalDto.STATE_W);
        goldWithdrawalPo.setStateMsg("待审核");
        goldWithdrawalPo.setApplyUserId(userId);
        goldWithdrawalPo.setCommunityId(goldDtos.get(0).getCommunityId());
        goldWithdrawalPo.setAmount(reqJson.getString("applyAmount"));
        goldWithdrawalPo.setGoldName(goldDtos.get(0).getGoldName());
        int flag = goldWithdrawalV1InnerServiceSMOImpl.saveGoldWithdrawal(goldWithdrawalPo);

        if (flag < 1) {
            throw new CmdException("保存数据失败");
        }

        context.setResponseEntity(ResultVo.success());
    }
}
